/***
*
* File: scoreboarddm.nut
* Auth: Damian Wojdat (original author: Sebihunter)
* Date: 20.10.2012
* Desc: Shows players stats.
*
*************************************************************************************************************/

//Definitions
const MAX_PLAYERS = 32;
local maxPlayers = 48 //This needs to be changed if your server allows more than 48 players!
local playersKills = {};
local playersDeaths = {};
local playersPoints = {};
local playersFactions = {};
local serverName = "WParty IV" //Change it to your server name
local onlinePlayers = 0
local screen = guiGetScreenSize();
local showscoreboard = false;
local boldfont = GUIFont("tahoma-bold");
local font = GUIFont("tahoma");
local headerfont = GUIFont("bankgothic", 12);
local scoreBoardForceToggled = 0;
local faction1Kills;
local faction2Kills;
local faction3Kills;
const COLOR_LSPD = "0000FFAA";
const COLOR_GANG = "C2A2DAAA";
const COLOR_GANG2 = "FFFF00AA";

//Gets the playercount on the server
function getPlayersOnServer()
{
	local players = 0
	for(local ply = 0; ply < maxPlayers; ply++)
	{
		if(isPlayerConnected(ply))
		{
			players++;
		}
	}
	return players;
}

//Draw the scoreboard
function onFrameRender()
{
	if(scoreBoardForceToggled)
		triggerServerEvent("updatePlayersStats");
	if(showscoreboard == true || scoreBoardForceToggled)
	{
		local y = screen[1]/2-(15*onlinePlayers)/2;
		guiDrawRectangle(screen[0]/2-200, y, 450.0, 15.0, 0x50505080, false);
		y = y+15;
		guiDrawRectangle(screen[0]/2-200, y, 450.0, 15.0+onlinePlayers*15+75.0, 0x00000080, false);
		boldfont.drawText(screen[0]/2-200+1, y,"ID", false);
		boldfont.drawText(screen[0]/2-200+50, y,"Name", false);
		boldfont.drawText(screen[0]/2-200+250, y,"Ping", false);	
		boldfont.drawText(screen[0]/2-200+300, y,"Kills", false);	
		boldfont.drawText(screen[0]/2-200+350, y,"Deaths", false);	
		boldfont.drawText(screen[0]/2-200+400, y,"Points", false);	
		y = y+15;
		local players = 0;
		for(local ply = 0; ply < maxPlayers; ply++)
		{
			if(isPlayerConnected(ply))
			{
				font.drawText(screen[0]/2-200+1, y, ply.tostring(), false);
				if(playersFactions[ply] == 0)
					font.drawText(screen[0]/2-200+50, y, "["+COLOR_LSPD+"]"+getPlayerName(ply), false);
				else if(playersFactions[ply] == 1)
					font.drawText(screen[0]/2-200+50, y, "["+COLOR_GANG+"]"+getPlayerName(ply), false);
				else if(playersFactions[ply] == 2)
					font.drawText(screen[0]/2-200+50, y, "["+COLOR_GANG2+"]"+getPlayerName(ply), false);
				local ping = getPlayerPing(ply);
				if (ping < 100) {
					font.drawText(screen[0]/2-200+250, y, "[00FF33FF]"+ping.tostring(), false);
				} else if (ping < 200){
					font.drawText(screen[0]/2-200+250, y, "[FF7D40FF]"+ping.tostring(), false);
				}else{
					font.drawText(screen[0]/2-200+250, y, "[CD0000FF]"+ping.tostring(), false);
				}	
				font.drawText(screen[0]/2-200+300, y, playersKills[ply].tostring(), false);
				font.drawText(screen[0]/2-200+350, y, playersDeaths[ply].tostring(), false);
				font.drawText(screen[0]/2-200+400, y, playersPoints[ply].tostring(), false);
				y = y+13;
				players++;
			}
		}
		y += 20;
		font.drawText(screen[0]/2-200+1, y, "Faction kills", false);
		y += 15;
		font.drawText(screen[0]/2-200+1, y, "Police: " + faction1Kills, false);
		y += 15;
		font.drawText(screen[0]/2-200+1, y, "Gang: " + faction2Kills, false);
		y += 15;
		font.drawText(screen[0]/2-200+1, y, "Mafia: " + faction3Kills, false);
		
		headerfont.drawText(screen[0]/2-200+1,screen[1]/2-(15*onlinePlayers)/2, serverName+" | Players: "+players.tostring(), false);
		onlinePlayers = players
	}	
}
addEvent("frameRender", onFrameRender);

function onKeyPress(key, status)
{
	if(key == "tab")
	{
		if(status == "down")
		{
			triggerServerEvent("updatePlayersStats");
		}
		else if(status == "up" && !scoreBoardForceToggled)
			hideScoreBoard();
	}
}
addEvent("keyPress", onKeyPress);

function hideScoreBoard()
{
	toggleChatWindow(true);
	showscoreboard = false;
}

function onPlayerStatsUpdate(playersKillsServer, playersDeathsServer, playersPointsServer, playersFactionsServer, playersCount, faction1KillsServer, faction2KillsServer, faction3KillsServer)
{
	for(local i = 0; i < playersCount; i++)
	{
		playersKills[i] <- playersKillsServer[i].tointeger();
		playersDeaths[i] <- playersDeathsServer[i].tointeger();
		playersPoints[i] <- playersPointsServer[i].tointeger();
		playersFactions[i] <- playersFactionsServer[i].tointeger();
	}
	faction1Kills = faction1KillsServer;
	faction2Kills = faction2KillsServer;
	faction3Kills = faction3KillsServer;
	toggleChatWindow(false);
	onlinePlayers = getPlayersOnServer()
	showscoreboard = true;
}
addEvent("playerStatsUpdate", onPlayerStatsUpdate);

function onScoreBoardForceToggle(toggle)
{
	if(toggle)
		scoreBoardForceToggled = 1;
	else
	{
		scoreBoardForceToggled = 0;
		hideScoreBoard();
	}
}
addEvent("scoreBoardForceToggle", onScoreBoardForceToggle);